package com.announcement.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.util.HtmlUtils;

import com.announcement.entity.Message;
import com.announcement.entity.User;
import com.announcement.entity.vo.Page;
import com.announcement.sevice.MessageService;
import com.announcement.sevice.UserService;
import com.announcement.util.SubStringHTML;

@RequestMapping("/message")
@Controller
public class MessageController {

	@Autowired
	private MessageService messageService;
	@Autowired
	private UserService userService;
	private User OnlineUser;

	@ModelAttribute
	public void checkUser(HttpServletRequest req, HttpServletResponse res,
			Model model) {
		OnlineUser = (User) req.getSession().getAttribute("OnlineUser");
	}

	@RequestMapping(value = "/articles", method = RequestMethod.GET)
	public String articles() {
		return "articles";
	}

	@RequestMapping(value = "/toEidtArticle", method = RequestMethod.GET)
	public String toEidtArticle() {
		if (OnlineUser == null) {
			return "redirect:/user/login";
		}
		return "editarticle";
	}

	@RequestMapping(value = "/addArticle", method = RequestMethod.POST)
	public String addArticle(Message msg, HttpServletRequest req,
			HttpServletResponse res, Model model) {
		if (OnlineUser == null) {
			return "redirect:/user/login";
		}

		msg.setUser(OnlineUser);
		boolean status = messageService.addArticle(msg);

		if (status) {
			model.addAttribute("tip", "发布失败");
			model.addAttribute("jumpurl",
					"http://localhost:8080/Announcement/editarticle.jsp");
		}
		model.addAttribute("tip", "发布成功");
		model.addAttribute("jumpurl",
				"http://localhost:8080/Announcement/message/showArticleToHome?pageIndex=1");
		return "tip";
	}

	@RequestMapping(value = "/delete/{messId}", method = RequestMethod.GET)
	public String deleteArticle(HttpServletRequest req,
			HttpServletResponse res, @PathVariable int messId) {

		boolean status = messageService.deleteArticle(messId);

		
		return "redirect:/message/showMyArticle";
	}

	@RequestMapping(value = "/update/{messId}", method = RequestMethod.GET)
	public String updateArticle(HttpServletRequest req,
			HttpServletResponse res, @PathVariable int messId, Model model) {

		Message msg = messageService.showArticleById(messId);
		model.addAttribute("msg", msg);

		return "editarticle";
	}

	@RequestMapping(value = "/showMyArticle", method = RequestMethod.GET)
	public String showMyArticle(HttpServletRequest req,
			HttpServletResponse res, int pageIndex, Model model) {
		if (OnlineUser == null) {
			return "redirect:/user/login";
		}
		Page<Message> page = messageService.showMyArticle(pageIndex,
				OnlineUser.getUserId());

		for (int i = 0; i < page.getList().size(); i++) {
			String content = page.getList().get(i).getContent();
			content = HtmlUtils.htmlUnescape(content);
			content = SubStringHTML.subStringHTML(content, 100);
			page.getList().get(i).setContent(content);
		}

		model.addAttribute("page", page);
		System.out.println("Controller:" + page);
/*		System.out.println(page.getList().get(0).getUser().getUsername());*/

		return "myarticles";
	}

	@RequestMapping(value = "/showArticle/{messId}", method = RequestMethod.GET)
	public String showArticleById(HttpServletRequest req,
			HttpServletResponse res, @PathVariable int messId, Model model) {

		Message msg = messageService.showArticleById(messId);
		model.addAttribute("msg", msg);
		return "articledetail";
	}

	@RequestMapping(value = "/showArticleToHome", method = RequestMethod.GET)
	public String showArticleToHome(HttpServletRequest req,
			HttpServletResponse res, int pageIndex, Model model) {

		Page<Message> page = messageService.showArticleToHome(pageIndex);
		for (int i = 0; i < page.getList().size(); i++) {
			String content = page.getList().get(i).getContent();
			content = HtmlUtils.htmlUnescape(content);
			content = SubStringHTML.subStringHTML(content, 180);
			page.getList().get(i).setContent(content);
		}

		model.addAttribute("page", page);
		System.out.println("Controller:" + page);
		return "articles";
	}

	@RequestMapping(value = "/search", method = RequestMethod.POST)
	public String searchArticle(HttpServletRequest req,
			HttpServletResponse res, int pageIndex, Model model, String keywords) {

		Page<Message> page = messageService.searchArticle(pageIndex, keywords);
		for (int i = 0; i < page.getList().size(); i++) {
			String content = page.getList().get(i).getContent();
			content = HtmlUtils.htmlUnescape(content);
			content = SubStringHTML.subStringHTML(content, 180);
			page.getList().get(i).setContent(content);
		}
		model.addAttribute("page", page);
		return "articles";
	}

}
